﻿@{
    DBContext db = new DBContext();
    var list = db.FindList<Sys_RoleM>(null, orderby => new { orderby.Role_Num });
}


<div id="hzy-container">

    <div class="page-content">

        <div class="row">
            <div class="col-sm-10 col-sm-offset-1">

                <!--检索面板-->
                <div class="col-sm-6">
                    <div class="panel" style="height:calc(100vh - 52px)">
                        <div class="panel-body" style="height:calc(100vh - 160px); overflow-y:auto;">

                            <table class="table table-bordered table-default table-hover" id="table-roles">
                                <thead>
                                    <tr>
                                        <th>操作</th>
                                        <th>编号</th>
                                        <th>角色</th>
                                        <th>备注</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    @foreach (var item in list)
                                    {
                                        <tr onclick="$(this).find('.radio label').click()" style="cursor:pointer;">
                                            <td>
                                                <div class="radio">
                                                    <label onclick="App.GetRoleMenuFunctionTree('@item.Role_ID')">
                                                        <input type="radio" name="role" value="@item.Role_ID"> @item.Role_Name
                                                    </label>
                                                </div>
                                            </td>
                                            <td>@item.Role_Num</td>
                                            <td>@item.Role_Name</td>
                                            <td>@item.Role_Remark</td>
                                        </tr>
                                    }

                                </tbody>
                            </table>

                        </div>

                    </div>

                </div>

                <!--树-->
                <div class="col-sm-6">
                    <div class="panel" style="height:calc(100vh - 52px)">
                        <div class="panel-heading">
                            <h4>请勾选权限</h4>
                        </div>
                        <div class="panel-body" style="height:calc(100vh - 160px); overflow-y:auto;">
                            <!--标签树-->
                            <ul id="tree" class="ztree"></ul>

                        </div>
                        <div class="panel-footer text-right">
                            <button class="btn btn-primary btn-outline" onclick="App.Save()">保存</button>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>

</div>

@section css{
    <link href="~/Admin/lib/zTree/css/metroStyle/metroStyle.css" rel="stylesheet" />
    <link href="~/Admin/lib/icheck/skins/all.css" rel="stylesheet" />
    <style type="text/css">
        .panel {
            padding: 20px;
        }
    </style>
}
@section js{
    <script src="~/Admin/js/admin-list.js"></script>
    <script src="~/Admin/lib/icheck/icheck.min.js"></script>
    <script src="~/Admin/lib/zTree/js/jquery.ztree.all.min.js"></script>
    <script type="text/javascript">
        var _roleid;
        $(function () {
            $('input[type=radio]').iCheck({
                checkboxClass: 'icheckbox_flat-green',
                radioClass: 'iradio_flat-green'
            });
            //加载默认click 第一个 角色
            $("#table-roles .radio:first label").click();
        });

        var App = {

            GetRoleMenuFunctionTree: function (id) {
                _roleid = id;
                admin.ajax({
                    url: "@Url.Action("GetRoleMenuFunctionTree")",
                    data: {
                        roleid: id
                    },
                    success: function (r) {
                        var setting = {
                            check: {
                                enable: true,
                                chkboxType: { "Y": "ps", "N": "ps" }
                            },
                            view: {
                                dblClickExpand: true
                            },
                            data: {
                                simpleData: {
                                    enable: true,
                                    idKey: "id",
                                    pIdKey: "pId",
                                    rootPId: 0,
                                },
                                key: { checked: 'checked' }
                            },
                            callback: {
                                onClick: function (event, treeId, treeNode) {
                                    var treeObj = $.fn.zTree.getZTreeObj("tree");
                                    if (treeNode.checked == false && treeNode.tag == "fun") {
                                        treeObj.checkNode(treeNode, true, true);
                                    } else {
                                        treeObj.checkNode(treeNode, false, false);
                                    }
                                },
                            }
                        };
                        zTree = $.fn.zTree.init($("#tree"), setting, r.value);
                        zTree.expandAll(true);//展开所有
                    }
                });
            },
            Save: function () {
                var treeObj = $.fn.zTree.getZTreeObj("tree");
                var nodes = treeObj.getCheckedNodes(true);

                var array = [];

                for (var i = 0; i < nodes.length; i++) {
                    if (nodes[i].tag == "fun") {
                        array.push({
                            MenuFunction_MenuID: nodes[i].pId,
                            MenuFunction_FunctionID: nodes[i].id
                        });
                    }

                }

                //保存功能
                admin.ajax({
                    url: "@Url.Action("Save")",
                    data: {
                        rows: JSON.stringify(array),
                        roleid: _roleid
                    },
                    success: function (r) {
                        if (r.status == 1) {
                            App.GetRoleMenuFunctionTree(_roleid);
                            admin.msg("保存成功!", "成功");
                        }
                    }
                });
            }

        };

    </script>
}
